﻿/// <reference path="../Scripts/jquery-1.8.1-vsdoc.js" />

$.ajaxSetup({
    url: "/Handler/ProjectType.ashx?" + (new Date()).valueOf(),
    type: "POST",
    dataType: "text",
    global: false,
});

var ID;
$(document).ready(function () {
    BindProjectType();
    $("#btnSave").bind("click", Save);
    $("#btnDel").bind("click", Del);
    ID = $("#MainContent_ID").val();
    if (!Util.isEmpty(ID)) {
        $("#btnDel").show();
        BindData();
    }
});

function Save() {    
    if(!IsValidate())
    {
        return;
    }
    var ret=GetData();
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:Util.isEmpty(ret.ID)?"addData":"updateData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
            Util.freshPage("ProjectTypeList.aspx");
        }
    });
}

function Del()
{
    if(!confirm("确定删除?"))
    {
       return;
    }

    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val())
    }
    var jsonStr=$.toJSON(ret);
    $.ajax({
        contentType:'application/x-www-form-urlencoded;charset=gb2312',
        data: {action:"removeData",jsonDataString:jsonStr},
        beforeSend: function () {
            Util.blockUI("正在保存数据...");
        },
        success: function (data) {
            Util.unblockUI();
            Util.freshPage("ProjectTypeList.aspx");
        }
    });

}

function GetData() {
    var ret=
    {
       "ID":encodeURIComponent($("#MainContent_ID").val()),
       "ParentID":encodeURIComponent($("#ParentType").find("option:selected").val()),
       "Name": encodeURIComponent($("#Name").val()),
       "Memo":encodeURIComponent($("#Memo").val()),
       "LastModifiedBy":encodeURIComponent($("#MainContent_UserID").val()),
    }

    return ret;
}

function IsValidate() {
    var msg="";
    if (Util.isEmpty($('#Name').val())) {
        msg="名称不能为空!";
    }

    if(Util.isEmpty(msg))
    {
        return true;
    }
    else
    {
        alert(msg);
        return false;
    }
}

function BindProjectType()
{
        $.ajax({
            url: "/Handler/ProjectType.ashx?" + (new Date()).valueOf(),
            async:false,
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData"},
            success: function (data) {
            var ret=$.parseJSON(data).ProjectTypes;
            var selects= $("#ParentType");
            selects.empty();
            selects.append("<option value='-1'>-请选择-</option>"); 
            for (var i=0;i<ret.length;i++) 
            {
                selects.append("<option value='"+decodeURIComponent(ret[i].ID)+"'>"+decodeURIComponent(ret[i].Name)+"</option>"); 
            }
        }
    });
}


function BindData() {
    $.ajax({
            contentType:'application/x-www-form-urlencoded;charset=gb2312',
            data: {action:"loadData",id:ID},
            beforeSend: function () {
                Util.blockUI("正在加载数据...");
            },
            success: function (data) {
                Util.unblockUI();
                var obj=$.parseJSON(data).ProjectTypes[0];
                $("#MainContent_ID").val(decodeURIComponent(obj.ID));
                SelectedValue("ParentType",decodeURIComponent(obj.ParentID));
                $("#Name").val(decodeURIComponent(obj.Name));
                $("#Memo").val(decodeURIComponent(obj.Memo));
            }
        });
}